package com.tang.campus_market.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tang.campus_market.dao.OrderDao;
import com.tang.campus_market.entity.Order;
import com.tang.campus_market.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @ Author     ：Marine.Li
 * @ Date       ：Created in 14:21 2021/8/5
 * @ Description：
 * @ Modified By：
 * @Version: $
 */

@Service
public class OrderServiceImpl implements OrderService {
    
    @Autowired
    private OrderDao orderDao;
    
    @Override
    public List<Order> queryList(Map<String,Object> map) {
        EntityWrapper<Order> wrapper = new EntityWrapper<>();
        for (String key : map.keySet()) {
            wrapper.eq(map.get(key) != null, key, map.get(key));
        }
        return orderDao.selectList(wrapper);
    }

    @Override
    public Page<Order> selectOrderPage(Integer page, Integer rows) {
        EntityWrapper<Order> wrapper = new EntityWrapper<>();
        Page<Order> orderPage = new Page<>(page, rows);
        List<Order> orders = orderDao.selectPage(orderPage, wrapper);
        orderPage.setRecords(orders);
        return orderPage;
    }

    @Override
    public Order getById(int orderId) {
        return orderDao.selectById(orderId);
    }

    @Override
    public Order getByGoodId(int goodId) {
        return orderDao.findByGoodId(goodId);
    }

    @Override
    public int delete(int orderId) {
        return orderDao.deleteById(orderId);
    }

    @Override
    public List<Order> getOrderByCustomerId(int customerId) {
        EntityWrapper<Order> wrapper = new EntityWrapper<>();
        wrapper.eq("customer_id", customerId);
        return orderDao.selectList(wrapper);
    }

    @Override
    public List<Order> getOrderBySellerId(int sellerId) {
        EntityWrapper<Order> wrapper = new EntityWrapper<>();
        wrapper.eq("seller_id", sellerId);
        return orderDao.selectList(wrapper);
    }

    @Override
    public List<Order> getSellerOtherOrders(int sellerId, int orderId) {
        EntityWrapper<Order> wrapper = new EntityWrapper<>();
        wrapper.eq("seller_id", sellerId)
                .ne("id", orderId);
        return orderDao.selectList(wrapper);
    }

    @Override
    public List<Order> getCustomerOtherOrders(int customerId, int orderId) {
        EntityWrapper<Order> wrapper = new EntityWrapper<>();
        wrapper.eq("customer_id", customerId)
                .ne("id", orderId);
        return orderDao.selectList(wrapper);
    }

    @Override
    public Integer cancelOrder(int goodId, int orderId) {
        Order order = orderDao.selectById(orderId);
        if (order == null){
            throw new RuntimeException("订单不存在！");
        }
        if (order.getStatus() != 1){
            throw new RuntimeException("订单已完成或取消，请勿重复操作");
        } else {
            order.setStatus(3); //将订单状态设置为3，订单已取消
        }
        return orderDao.updateById(order);
    }

    @Override
    public Integer updateOrder(int orderId, int statusId) {
        Order order = orderDao.selectById(orderId);
        if (order == null){
            throw new RuntimeException("订单不存在！");
        }
        order.setStatus(statusId);
        return orderDao.updateById(order);
    }

    @Override
    public Order selectById(Integer orderId) {
        return orderDao.selectById(orderId);
    }

    @Override
    public Integer createOrder(Order order) {
        Order order1 = orderDao.selectById(order.getId());
        if (order1 != null){
            throw new RuntimeException("创建订单失败，请检查数据是否正确！");
        }
        SimpleDateFormat formator = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String submitDate = formator.format(new Date());
        order.setStatus(1);
        order.setSubmitDate(submitDate);
//        调用insert方法之后，order对象的id将会自动装填进去，不用手动查询了
        return orderDao.insert(order);
    }

    @Override
    public Integer complete(Integer orderId) {
        Order order = orderDao.selectById(orderId);
        if (order == null){
            throw new RuntimeException("订单不存在！");
        }
        if (order.getStatus() != 1){
            throw new RuntimeException("订单已完成或取消！");
        }
        order.setStatus(2);
        return orderDao.updateById(order);
    }

    @Override
    public List<Order> findAll() {
        return orderDao.findAll();
    }

    @Override
    public void deleteById(Integer id) {
        orderDao.deleteById(id);
    }
}
